package gray.android.button;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import gray.Frame;
import gray.android.FactoryAndroid;
import util.observer.Trigger;

public final class BButton extends LinearLayout implements Frame<View>, OnClickListener{
	private final Trigger _observer;
	private final Object _msg;
	private final Frame<View> _valid;
  private final Context _context;
	
	private BButton(Context context,
                  final Trigger observer, 
                  final String name,
                  final Character key, 
                  final Object msg, 
                  final Frame<View> valid) {
		super(context);
		android.widget.Button ok = new android.widget.Button(context);
		ok.setText(name);
		ok.setEnabled(true);
    if (observer!=null)
      ok.setOnClickListener(this);
    ok.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                            LayoutParams.WRAP_CONTENT));
		addView(ok);
    //ok.setTextColor(theme.backgroundColor());
		
		_observer = observer;
		_msg = msg;
		_valid = valid;
    _context=context;
	}

	public static Frame<View> factoryValideWithMsg(Context context, 
                                                 final Trigger observer, String name,
                                                 final Character key, 
                                                 final Object msg, 
                                                 final Frame<View> valid){
		return new BButton(context,observer,name,key,msg,valid);
	}
	
	@Override
	public View container() {
		return this;
	}

	@Override
	public Boolean ready() {
		return true;
	}

	@Override
	public void onClick(View arg0) {
    if (_valid!=null && !_valid.ready()){
      FactoryAndroid.message(_context, "some field are not valid");
    }
    else
      _observer.poke(_msg);

	}

}
